package com.seng.roleservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seng.roleservice.entity.TbRole;
import com.seng.roleservice.service.TbRoleService;
import com.seng.utilscommon.pwdmd5.MD5Utils;
import com.seng.utilscommon.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author seng
 * @since 2020-08-03
 */
@RestController
@RequestMapping("/roleservice/tb-role")
@CrossOrigin
public class TbRoleController {
    @Resource
    private TbRoleService tbRoleService;

    @ApiOperation(value = "查询所有角色信息")
    @PostMapping("/findAll/{current}/{size}")
    public Result findAll(@PathVariable("current") Integer current,
                          @PathVariable("size") Integer size,
                          @RequestBody(required = false) TbRole tbRole){
        Page<TbRole> page = new Page<>(current,size);
        QueryWrapper<TbRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        if (!StringUtils.isEmpty(tbRole.getRoleName())){
            queryWrapper.like("role_name",tbRole.getRoleName());
        }
        IPage<TbRole> studentPage = tbRoleService.page(page, queryWrapper);
        return Result.ok().data("rolese",studentPage);
    }

    @ApiOperation(value = "逻辑删除角色")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        System.out.println(id);
        boolean delete = tbRoleService.removeById(id);
        if (delete) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "新增角色信息")
    @PostMapping(value = "/addrole")
    public Result addUser(@RequestBody TbRole tbRole) {
        //差一个头像没有设置
        tbRole.setStatus(1);
        boolean user = tbRoleService.save(tbRole);
        if (user) {
            return Result.ok().data("add", "ok");
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "根据id进行修改")
    @PutMapping("/updateById")
    public Result updateById(@RequestBody TbRole tbRole) {
        System.out.println(tbRole.toString());
        boolean updatebyId = tbRoleService.updateById(tbRole);
        //System.out.println(tbUserService.getById(tbUser.getId()));
        /*if (!updatebyId) {
            throw new RuntimeException("角色没找到");
        }*/
        return Result.ok().data("updatebyId", updatebyId);
    }

    @ApiOperation(value = "导出角色信息")
    @GetMapping(value = "/export")
    public void exportStudentInfoExcel(HttpServletResponse response) throws IOException {
        tbRoleService.exportStudentInfoExcel(response);
    }
}

